﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Security.Cryptography;
using System.Text;
using System.IO;

public partial class APP_frmA_TraineeList : System.Web.UI.Page
{

    //static private byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 }; 
    //static private byte[] iv8Bit = { 1, 2, 3, 4, 5, 6, 7, 8 };
    private static string EncryptionKey = "AT853ggdt";
    private static byte[] key = { };
    private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    



    protected void Page_Load(object sender, EventArgs e)
    {
        ((APP_MasterPage_appCom)this.Master).changeTitle("技能実習生一覧");
        ((APP_MasterPage_appCom)this.Master).HideLiArea("frmA_TraineeList");
        if (!IsPostBack)
        {
            if (Session["役"] != null)
            {
                if (Session["役"].ToString() == "1")
                {
                    
                    this.ShowSysAdmin();
                }
                else
                {
                    DropDownList1.Enabled = false;    
                    this.Show();
                }
            }
            else
            {
                Response.Redirect("~/frmD_Login.aspx");
            }
           
           
        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "registerControlToValidation(\"validation\");Disable();registerEnter();", true);
        if (Session["役"] != null)
        {
            if (Session["役"].ToString() == "1")
            {
                DropDownList1.Focus();
                validateddl_DropDownList1.ErrorMessage =  GetError("ER00007");
                RequiredFieldValidator1.ErrorMessage =  GetError("ER00007");
            }
            else
            {
                ddl_検索.Focus();
                DropDownList1.Enabled = false;
                validateddl_DropDownList1.Enabled = false;
                RequiredFieldValidator1.ErrorMessage = "" + GetError("ER00007");
            }
        }

        //Response.Write(Session["役"].ToString());
        //Response.Write(Session["企業コード_2"].ToString());
        //TextBox1.Focus();
    }

    public String GetError(string errorid) // ham dung de lay ma loi hien thi thong tin cua loi do.
    {
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        return dt.Rows[0][2].ToString();
    }
    protected void btn_技能実習生個人情報_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmA_TraineeBasicInfo.aspx");
    }
    protected void btn_送出し機関情報_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmA_JointCompBasicInfoRef.aspx");
    }
    public void Show() //  tim kiem danh sach cac TNS cua cong ty tiep nhan
    {
        try
        {
            //int 企業ID;
                DropDownList1.Visible = false;
                基本情報Manager frm = new 基本情報Manager();
                DataTable dt = new DataTable();
                //string 役 = Session["役"].ToString();
                //if (役 == "1")
                //{
                int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());
                //}
                int 検索 = Int32.Parse(ddl_検索.SelectedValue.ToString());
                string a = TextBox1.Text.Trim().ToString();
                dt = frm.SearchTraineeList(企業ID, 検索, TextBox1.Text.ToString());
                if (dt.Rows.Count > 0)
                {
                    grv.Visible = true;
                    grv.DataSource = dt;
                    grv.DataBind();
                    label1.Text = "※氏名をクリックすると、個人の詳細情報を確認できます。";
                    GridView1.Visible = false;
                }
                else
                {
                    grv.Visible = false;
                    GridView1.Visible = true;
                    AppCode_Common.ShowEmtyGrid<本社履歴>(GridView1, 7, "データがありません。", "");
                    label1.Text = "";
                    //label1.Visible = false;

                }
            
        }
        catch { }
    }

    public void ShowSysAdmin()
    {
         string 役 = Session["役"].ToString();
         bindDropdownList1();
         //int 企業ID = Int32.Parse(DropDownList1.SelectedValue);
         //Response.Write(Convert.ToString(企業ID));
         grv.Visible = false;
         GridView1.Visible = true;
         label1.Text = ""; 
        //label1.Visible = false;
         AppCode_Common.ShowEmtyGrid<本社履歴>(GridView1, 7, "データがありません。", "");
         ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "registerControlToValidation(\"validation\");Disable();registerEnter();", true);
            
    }

    public void bindDropdownList1()
    {
        本社情報Manager frm1 = new 本社情報Manager();
        DataTable dt1 = new DataTable();
        dt1 = frm1.Select_TraineeList();
        DropDownList1.DataSource = dt1;
        DropDownList1.DataTextField = "企業名";
        DropDownList1.DataValueField = "企業ID";
        DropDownList1.DataBind();
        ListItem item = new ListItem("-選択-", "-1");
        item.Selected = true;
        DropDownList1.Items.Insert(0, item);
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "registerControlToValidation(\"validation\");Disable();registerEnter();", true);
    }
    public string ConvertDate(string str)// ham convert nagy thang nam.
    {
        if (str == "")
        {
            return str = "";
        }
        else
        {
            string date = str;
            DateTime d1 = DateTime.Parse(date);
            d1.Year.ToString();
            return date = d1.ToString("yyyy年MM月dd日");
        }
    }
    public string ConvertString(string str)// ham bien doi chuoi.
    {
        
        if (str.Equals("True"))
        {
             return "女";
        }
        else if (str == "False")
        {
             return "男";
        }
        else
        {
            return "";
        }
        
    }

    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)// phan trang.
    {
        grv.PageIndex = e.NewPageIndex;
        
        if (Session["役"] != null)
        {
            if (Session["役"].ToString() == "1")
            {
                this.ShowSysAdminPageIndex();
            }
            else
            {
                this.Show();
            }
        }
    }

    public void ShowSysAdminPageIndex()
    {
        基本情報Manager frm = new 基本情報Manager();
        DataTable dt = new DataTable();
        int 企業ID = Int32.Parse(DropDownList1.SelectedValue);
        int 検索 = Int32 .Parse(ddl_検索.SelectedValue.ToString());
        string search = TextBox1.Text.Trim().ToString();
        search = search.Replace("<", "&lt;");
        search = search.Replace(">", "&gt;");
        search = search.Replace("'", "&rsquo;");
        dt = frm.SearchTraineeList(企業ID, 検索, search);
        if (dt.Rows.Count > 0)
        {
            grv.Visible = true;
            GridView1.Visible = false;
            grv.DataSource = dt;
            grv.DataBind();
            label1.Text = "※氏名をクリックすると、個人の詳細情報を確認できます。";
        }
        else
        {
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "<script>alert('データがありません');</script>", false);

            grv.Visible = false;
            GridView1.Visible = true;
            //grv.DataSource = dt;
            //grv.DataBind();
            //TextBox1.Focus();
            label1.Text = "";
            //label1.Visible = false;
            AppCode_Common.ShowEmtyGrid<本社履歴>(GridView1, 7, "データがありません。", "");

            //return;

        }
    }
    protected void btn_検索_Click(object sender, EventArgs e)
    {
        try
        {
            string 役 = Session["役"].ToString();
            if (役 == "1")
            {

                基本情報Manager frm = new 基本情報Manager();
                DataTable dt = new DataTable();
                int 企業ID = Int32.Parse(DropDownList1.SelectedValue); 
                int 検索 = Int32.Parse(ddl_検索.SelectedValue.ToString());
                string search = TextBox1.Text.Trim().ToString();
                search = search.Replace("<", "&lt;");
                search = search.Replace(">", "&gt;");
                search = search.Replace("'", "&rsquo;");
                dt = frm.SearchTraineeList(企業ID, 検索, search);
                if (dt.Rows.Count > 0)
                {
                    grv.Visible = true;
                    GridView1.Visible = false;
                    grv.DataSource = dt;
                    grv.DataBind();
                    label1.Text = "※氏名をクリックすると、個人の詳細情報を確認できます。";
                }
                else
                {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "<script>alert('データがありません');</script>", false);

                    grv.Visible = false;
                    GridView1.Visible = true;
                    //grv.DataSource = dt;
                    //grv.DataBind();
                    //TextBox1.Focus();
                    label1.Text = "";
                    //label1.Visible = false;
                    AppCode_Common.ShowEmtyGrid<本社履歴>(GridView1, 7, "データがありません。", "");

                    //return;

                }
            }
            else
            {


                基本情報Manager frm = new 基本情報Manager();
                DataTable dt = new DataTable();
                int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());
                int 検索 = Int32.Parse(ddl_検索.SelectedValue.ToString());
                string search = TextBox1.Text.Trim().ToString();
                search = search.Replace("<", "&lt;");
                search = search.Replace(">", "&gt;");
                search = search.Replace("'", "&rsquo;");
                dt = frm.SearchTraineeList(企業ID, 検索, search);
                if (dt.Rows.Count > 0)
                {
                    grv.Visible = true;
                    GridView1.Visible = false;
                    grv.DataSource = dt;
                    grv.DataBind();
                    label1.Text = "※氏名をクリックすると、個人の詳細情報を確認できます。";
                }
                else
                {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "<script>alert('データがありません');</script>", false);

                    grv.Visible = false;
                    GridView1.Visible = true;
                    //grv.DataSource = dt;
                    //grv.DataBind();
                    //TextBox1.Focus();
                    label1.Text = "";
                    AppCode_Common.ShowEmtyGrid<本社履歴>(GridView1, 7, "データがありません。", "");

                    //return;

                }
            }
        }
        catch { }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "registerControlToValidation(\"validation\");Disable();registerEnter();", true);
    }
    protected void lk1_氏名_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmA_TraineeBasicInfo.aspx");
    }


    protected void lk2_送出し機関名_Click(object sender, CommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        string url = "frmA_JointCompBasicInfoRef.aspx?";
        url += "ID=" + ID;
        Response.Redirect(url);
    }
    //protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    try
    //    {
    //        int ID = Int32.Parse((e.CommandArgument).ToString());
            
    //        string url = "frmA_TraineeBasicInfo.aspx?";
    //        url += "ID=" +ID;
    //        Response.Redirect(url);
    //    }
    //    catch { }
    //}

    protected void lk1_Click(object sender, CommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        
        string url = "frmA_TraineeBasicInfo.aspx?";
        url += "ID=" + ID;
        Response.Redirect(url);
    }

    //private string base64Encode(string sData) 
    //{ 
    //    try 
    //    { 
    //        byte[] encData_byte = new byte[sData.Length]; 
    //        encData_byte = System.Text.Encoding.UTF8.GetBytes(sData); 
    //        string encodedData = Convert.ToBase64String(encData_byte); 
    //        return encodedData; } catch (Exception ex) 
    //    { 
    //            throw new Exception("Error in base64Encode" + ex.Message); 
    //    } 
    //}


    public static string base64Encode(string strPassword)
    {
        FormsAuthenticationTicket Ftk = new FormsAuthenticationTicket(strPassword, true, 600);

        return FormsAuthentication.Encrypt(Ftk);
        
          //string password = FormsAuthentication.HashPasswordForStoringInConfigFile("password", "SHA1");

        //TripleDES threedes = new TripleDESCryptoServiceProvider();
        //threedes.Key = StringToByte("m65hhhgfd", 24);
        //threedes.IV = StringToByte("12345678");
        //byte[] key = threedes.Key;
        //byte[] IV = threedes.IV;
        //ICryptoTransform encryptor = threedes.CreateEncryptor(key, IV);
        //MemoryStream msEncrypt = new MemoryStream();
        //CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
        //csEncrypt.Write(StringToByte(strPassword), 0, StringToByte(strPassword).Length);
        //csEncrypt.FlushFinalBlock();
        //byte[] encrypted = msEncrypt.ToArray();
        //return strPassword = ByteToString(encrypted);

        
        
        //var bytes = Encoding.Default.GetBytes(strPassword); 
        //using (var tripleDes = new TripleDESCryptoServiceProvider()) 
        //{ 
        //    using (var ms = new MemoryStream())
        //    using (var encryptor = tripleDes.CreateEncryptor(key, iv8Bit))
        //    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) 
        //    { 
        //        cs.Write(bytes, 0, bytes.Length); 
        //        cs.FlushFinalBlock(); var cipher = ms.ToArray(); 
        //        return Convert.ToBase64String(cipher); 
        //    } 
        //} 

        //key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 8));
        //DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        //Byte[] inputByteArray = Encoding.UTF8.GetBytes(strPassword);
        //MemoryStream ms = new MemoryStream();
        //CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
        //cs.Write(inputByteArray, 0, inputByteArray.Length);
        //cs.FlushFinalBlock();
        //return Convert.ToBase64String(ms.ToArray());


        //var bytes = System.Text.Encoding.UTF8.GetBytes(strPassword); var encBytes = System.Security.Cryptography.ProtectedData.Protect(bytes, new byte[0], System.Security.Cryptography.DataProtectionScope.LocalMachine); return Convert.ToBase64String(encBytes); 



       
        

    }

    public static byte[] StringToByte(string StringToConvert, int length)
    {

        char[] CharArray = StringToConvert.ToCharArray();
        byte[] ByteArray = new byte[length];
        for (int i = 0; i < CharArray.Length; i++)
        {
            ByteArray[i] = Convert.ToByte(CharArray[i]);
        }
        return ByteArray;
    }

    public static byte[] StringToByte(string StringToConvert)
    {

        char[] CharArray = StringToConvert.ToCharArray();
        byte[] ByteArray = new byte[CharArray.Length];
        for (int i = 0; i < CharArray.Length; i++)
        {
            ByteArray[i] = Convert.ToByte(CharArray[i]);
        }
        return ByteArray;
    }

    public static string ByteToString(CryptoStream buff)
    {
        string sbinary = "";
        int b = 0;
        do
        {
            b = buff.ReadByte();
            if (b != -1) sbinary += ((char)b);

        } while (b != -1);
        return (sbinary);
    }
    public static string ByteToString(byte[] buff)
    {
        string sbinary = "";
        for (int i = 0; i < buff.Length; i++)
        {
            sbinary += buff[i].ToString("X2"); // hex format
        }
        return (sbinary);
    }






     
}
